var cmd = function() {
    this.count = 0;
};
cmd.prototype = {};

/**
 * 登录
 */
cmd.prototype.signin = function(uid, password, source, success, fail) {
    var self = this;
    Util.resetSid();
    self.post("user/sign-in", {
        uid: uid,
        password: password,
        type: 1,
        source: source,
    }, success, fail);
}

/**
 * 登出
 */
cmd.prototype.signout = function(cb) {
    var self = this;
    self.post("user/sign-out", {}, cb);
}

/**
 * 当前用户的状态
 */
cmd.prototype.user_online_sid = function(cb) {
    var self = this;
    self.post("user/online/find-by-sid", {}, cb);
}

/**
 * 上传图片
 */
cmd.prototype.upload = function(data, url, cb) {
    if (!url) {
        url = Util.getUrl("tephra/ctrl-http/upload");
        url = url.replace(context.config.url, window.location.origin + "/");
        data.sid = Util.sid;
    }

    $.ajax({
        url: url,
        type: "POST",
        data: data,
        processData: false,
        contentType: false,
        success: function(r) {
            if (r) {
                var arr = r.split(",");
                cb(arr[0]);
            }
        }
    });
}

/**
 * 获取账变记录
 */
cmd.prototype.query_account_log = function(data, cb) {
    var self = this;
    self.post("account/log/query", data, function(r) {
        var list = r.list;
        for (var i in list) {
            if (list[i].type == 'consume' || list[i].type == "profit" || list[i].type == "cancel" ||
                list[i].type == 'back') {
                list[i].memo = get_game_display_name(list[i].lottery) + " " + list[i].issue + "期 (" +
                    Util.convertTargets(list[i].lottery, list[i].target) + ")"; 
            }
        }
        r.list = list;
        cb(r);
    });
}
cmd.prototype.query_account_log2 = function(data, cb) {
    var self = this;
    self.post("account/log/uquery", data, function(r) {
        var list = r.list;
        for (var i in list) {
            if (list[i].type == 'consume' || list[i].type == "profit" || 
                list[i].type == "cancel" || list[i].type == 'back') {
                list[i].memo = get_game_display_name(list[i].lottery) + " " + list[i].issue + "期 (" +
                    Util.convertTargets(list[i].lottery, list[i].target) + ")"; 
            }
        }
        r.list = list;
        cb(r);
    });
}
cmd.prototype.gquery_account_log = function(data, cb) {
    var self = this;
    self.post("account/log/gquery", data, function(r) {
        var list = r.list;
        for (var i in list) {
            if (list[i].type == 'consume' || list[i].type == "profit" || list[i].type == "cancel") {
                list[i].memo = get_game_display_name(list[i].lottery) + " " + list[i].issue + "期 (" +
                    Util.convertTargets(list[i].lottery, list[i].target) + ")"; 
            }
        }
        r.list = list;
        cb(r);
    });
}
cmd.prototype.account_log_rquery = function(data, cb) {
    var self = this;
    self.post("account/log/rquery", data, function(r) {
        var list = r.list;
        for (var i in list) {
            if (list[i].type == 'consume' || list[i].type == "profit" || list[i].type == "cancel") {
                list[i].memo = get_game_display_name(list[i].lottery) + " " + list[i].issue + "期 (" +
                    Util.convertTargets(list[i].lottery, list[i].target) + ")"; 
            }
        }
        r.list = list;
        cb(r);
    });
}

/**
 * 统计某个游戏/所有游戏的输赢信息
 */
cmd.prototype.bet_sum_gquery = function(type, start, end, group, cb) {
    var self = this;
    var data = {};
    if (type) data.type = type;
    if (group) data.owner = group;
    if (start) {
        if (start <= end) {
            data.start = start;
            data.end = end;
        }
        else {
            data.start = end;
            data.end = start;
        }
    }
    self.post("bet/sum/gdquery", data, cb);
}
cmd.prototype.bet_sum_gmquery = function(type, start, end, group, cb) {
    var self = this;
    var data = {};
    if (type) data.type = type;
    if (group) data.owner = group;
    if (start) {
        if (start <= end) {
            data.start = start;
            data.end = end;
        }
        else {
            data.start = end;
            data.end = start;
        }
    }
    self.post("bet/sum/gmquery", data, cb);
}

/**
 * 保存红包牛牛群的信息
 */
cmd.prototype.lottery_niuniu_save = function(group, data, cb) {
    var self = this;
    data.group = group;
    self.post("lottery/niuniu/save", data, cb);
}
cmd.prototype.lottery_niuniu_find = function(group, cb) {
    var self = this;
    self.post("lottery/niuniu/find", { group: group }, cb);
}

/**
 * 修改用户等级
 */
cmd.prototype.set_user_grade = function(id, grade, cb) {
    var self = this;
    self.post("user/grade", {
        id: id,
        grade: grade,
    }, cb);
}

/**
 * 用户的日报表（查询自己）
 */
cmd.prototype.stat_user_uquery = function(type, start, end, pageSize, pageNum, cb) {
    var self = this;
    var data = {
        pageSize: pageSize || 15,
        pageNum: pageNum || 1,
    };
    if (type) data.type = type;
    if (start) data.start = start;
    if (end) data.end = end;
    self.post("stat/user/uquery", data, cb);
}

/**
 * 获取用户的账号余额（群）
 */
cmd.prototype.account_gquery_uid = function(uid, cb) {
    var self = this;
    self.post("account/gquery-uid", {
        uid: uid,
    }, cb);
}

/**
 * 检索站内消息
 */
cmd.prototype.notice_query = function(user, type, count, pageSize, pageNumber, cb) {
    var self = this;
    var data = {
        pageSize: pageSize || 200,
        pageNum: pageNumber || 1,
    };
    if (count) data.count = count;
    if (user !== undefined) data.uid = user;
    if (type) data.type = type;
    self.post("notice/query", data, cb);
}
cmd.prototype.notice_uquery = function(type, unread, pageSize, pageNumber, cb) {
    var self = this;
    var data = {
        pageSize: pageSize || 200,
        pageNum: pageNumber || 1,
    };
    if (type) data.type = type;
    if (unread !== undefined) data.unread = unread;
    self.post("notice/uquery", data, cb);
}

/**
 * 标记通知已读
 */
cmd.prototype.notice_flag_read = function(id) {
    var self = this;
    self.post("link/save", {
        type: "notice",
        id1: context.user.user.id,
        id2: id,
    });
}

/**
 * 修改站内消息
 */
cmd.prototype.notice_save = function(data, cb) {
    var self = this;
    self.post("notice/save", data, cb);
}

/**
 * 删除站内消息
 */
cmd.prototype.notice_delete = function(id, cb) {
    var self = this;
    self.post("notice/delete", { id: id, }, cb);
}

/**
 * 账变记录审核通过
 */
cmd.prototype.pass_account_log = function(id, cb) {
    var self = this;
    self.post("account/log/pass", {
        ids: id,
    }, cb);
}
cmd.prototype.gpass_account_log = function(id, cb) {
    var self = this;
    self.post("account/log/gpass", {
        ids: id,
    }, cb);
} 

/**
 * 账变记录审核不通过
 */
cmd.prototype.reject_account_log = function(id, memo, cb) {
    var self = this;
    self.post("account/log/reject", {
        ids: id,
        memo: JSON.stringify(memo),
    }, cb);
}
cmd.prototype.greject_account_log = function(id, reason, cb) {
    var self = this;
    self.post("account/log/greject", {
        ids: id,
        memo: reason || '',
    }, cb);
}

/**
 * 下注列表
 */
cmd.prototype.query_bet = function(game, owner, data, cb) {
    var self = this;
    if (game) data.type = game;
    data.owner = owner || "";
    self.post("bet/query", data, cb);
}
cmd.prototype.gquery_bet = function(game, owner, data, cb){
    var self = this;
    if (game) data.type = game;
    if (owner) data.owner = owner;
    self.post("bet/gquery", data, cb);
}

cmd.prototype.query_bet_by_user = function(game, data, cb) {
    var self = this;
    if (game) data.type = game;
    self.post("bet/uquery", data, cb);
}
cmd.prototype.bet_rquery = function(data, cb) {
    var self = this;
    self.post("bet/rquery", data, cb);
}

/**
 * 代理者列出自己的代理会员
 */
cmd.prototype.user_rquery = function(data, cb) {
    var self = this;
    self.post("user/rquery", data, cb);
}

/**
 * 请求下注
 */
cmd.prototype.create_bet = function(game, issue, data, success, fail) {
    var self = this;
    data.type = game;
    data.issue = issue;
    self.post("bet/create", data, success, fail);
}

/**
 * 未结算的下注单
 */
cmd.prototype.bet_incomplete = function(cb) {
    var self = this;
    self.post("bet/incomplete", {}, cb);
}

/**
 * 今日已结算
 */
cmd.prototype.bet_today = function(game, cb) {
    var self = this;
    self.post("bet/today-complete", { type: game, pageSize: 400, }, cb);
}
cmd.prototype.bet_today_complete = function(game, pageNum, pageSize, cb) {
    var self = this;
    var data = {
        pageSize: pageSize || 200,
        pageNum: pageNum || 1,
    }
    if (game) data.type = game;
    self.post("bet/today-complete", data, cb);
}

/**
 * 修改群信息
 */
cmd.prototype.modify_group = function(group, data, cb) {
    self = this;
    data.id = group;
    self.post("group/modify", data, cb);
}

/**
 * 设置赔率
 */
cmd.prototype.group_lottery_rate_save = function(type, owner, data, cb) {
    var self = this;
    data.type = type;
    data.owner = owner;
    self.post("lottery/rate/gsaves", data, cb);
}

/**
 * 设置返水
 */
cmd.prototype.group_lottery_back_save = function(type, owner, data, cb) {
    var self = this;
    data.type = type;
    data.owner = owner;
    self.post("lottery/rate/gbacks", data, cb);
}

/**
 * 设置单注的限额
 */
cmd.prototype.group_lottery_rate_ranges = function(type, owner, data, cb) {
    var self = this;
    data.type = type;
    data.owner = owner;
    self.post("lottery/rate/granges", data, cb);
}

/**
 * 设置群的抽佣比例
 */
cmd.prototype.group_set_fee = function(id, fee, cb) {
    var self = this;
    self.post("group/fee", {
        id: id,
        fee: fee,
    }, cb);
}

/**
 * 代理统计
 */
cmd.prototype.stat_user_referee = function(data, cb) {
    var self = this;
    self.post("stat/user/referee", data, cb);
}

/**
 * 当前代理用户的统计信息
 */
cmd.prototype.stat_referee_query_user = function(data, cb) {
    var self = this;
    self.post("stat/referee/query-user", data, cb);
}
cmd.prototype.stat_referee_stat = function(cb) {
    var self = this;
    self.post("stat/referee/stat", {}, cb);
}

/**
 * 充值
 */
cmd.prototype.deposit = function(uid, owner, amount, channel, channel2, memo, cb) {
    var self = this;
    var data = {
        channel: channel,
        channel2: channel2,
    };
    if (uid) data.uid = uid;
    data.owner = owner || "";
    data.amount = amount;
    if (memo) {
        if (typeof(memo) == 'string')
            data.memo = memo;
        else
            data.memo = JSON.stringify(memo);
    }
    self.post("account/deposit", data, cb);
}

/**
 * 奖励
 */
cmd.prototype.reward = function(uid, owner, amount, memo, cb) {
    var self = this;
    var data = {
    };
    if (uid) data.uid = uid;
    data.owner = owner || "";
    data.amount = amount;
    if (memo) {
        if (typeof(memo) == 'string')
            data.memo = memo;
        else
            data.memo = JSON.stringify(memo);
    }
    self.post("account/reward", data, cb);
}

/**
 * 根据code查找群信息
 */
cmd.prototype.find_group_by_code = function(code, success, fail) {
    var self = this;
    self.post("group/find", {
        code: code,
    }, function(list) {
        if (Util.isEmpty(list)) {
            if (fail) fail();
        }
        else success(list);
    });
}

/**
 * 根据code查找用户
 */
cmd.prototype.find_user_by_code = function(code, success, fail) {
    var self = this;
    self.post("user/find", {
        code: code,
    }, success, fail);
}
cmd.prototype.find_user_by_uid = function(uid, success, fail) {
    var self = this;
    self.post("user/find", {
        uid: uid,
    }, success, fail);
}

/**
 * 拉取统计信息
 */
cmd.prototype.stat_sum_dquery = function(start, end, cb) {
    var self = this;
    var data = {
        owner: "",
    };
    if (start) data.start = start;
    if (end) data.end = end;
    self.post("stat/sum/dquery", data, cb);
}
cmd.prototype.stat_sum_mquery = function(start, end, cb) {
    var self = this;
    var data = {
        owner: "",
    };
    if (start) data.start = start;
    if (end) data.end = end;
    self.post("stat/sum/mquery", data, cb);
}

cmd.prototype.stat_sum_gdquery = function(start, end, cb) {
    var self = this;
    var data = {
    };
    if (start) data.start = start;
    if (end) data.end = end;
    self.post("stat/sum/gdquery", data, cb);
}
cmd.prototype.stat_sum_gmquery = function(start, end, cb) {
    var self = this;
    var data = {
    };
    if (start) data.start = start;
    if (end) data.end = end;
    self.post("stat/sum/gmquery", data, cb);
}

/**
 * 根据用户名获取账户信息
 */
cmd.prototype.find_user_by_name = function(uid, cb) {
    var self = this;
    self.post("user/query", {
        uid: uid,
        pageSize: 100
    }, cb);
}

/**
 * 根据微信昵称查找用户
 */
cmd.prototype.find_user_by_nick = function(name, cb) {
    var self = this;
    self.post("user/query", {
        nick: name,
        pageSize: 100,
    }, cb);
}

/**
 * 修改登录密码
 */
cmd.prototype.modify_password = function(password, old, cb, fail) {
    var self = this;
    self.post("user/password", {
        old: old, 
        new: password, 
        repeat: password,
    }, cb, fail);
}

/**
 * 拉取群的所有成员
 */
cmd.prototype.query_group_members = function(id, cb) {
    var self = this;
    self.post("group/member/query-by-group", {
        group: id,
    }, function(list) {
        cb(list);
    });
}

/**
 * 获取我所有的群
 */
cmd.prototype.query_groups = function(cb) {
    var self = this;
    self.post("group/query-by-user", {}, cb);
}

/**
 * 设置群的头像
 */
cmd.prototype.set_group_portrait = function(id, portrait, cb) {
    var self = this;
    self.post("group/portrait", {
        id: id,
        portrait: portrait
    }, cb);
}

/**
 * 设置群的公告内容
 */
cmd.prototype.set_group_board = function(id, content, cb) {
    var self = this;
    self.post("group/note", {
        id: id,
        note: content,
    }, cb);
}

/**
 * 修改群的名称
 */
cmd.prototype.set_group_name = function(id, name, cb) {
    var self = this;
    self.post("group/name", {
        id: id,
        name: name,
    }, cb);
}

/**
 * 设置群组的上限
 */
cmd.prototype.set_group_limit = function(id, limit, cb) {
    var self = this;
    self.post("group/limit", {
        id: id,
        limit: limit,
    }, cb);
}

/**
 * 获取开奖结果
 */
cmd.prototype.lottery_datas = function(data, cb) {
    var self = this;
    if (! data.size) data.size = 20;
    self.post("lottery/datas", data, cb);
}
cmd.prototype.lottery_query = function(data, cb) {
    var self = this;
    self.post("lottery/query", data, function(r) {
        for (var i in r.list) {
            r.list[i].red = r.list[i].red.split(",");
        }
        if (cb) cb(r);
    });
}

/**
 * 最近一期的信息
 */
cmd.prototype.lottery_next = function(game, cb) {
    var self = this;
    self.post("lottery/next", {
        type: game,
    }, cb);
}

/**
 * 获取某期的开奖结果
 */
cmd.prototype.lottery_data = function(game, issue, cb) {
    var self = this;
    self.post("lottery/data", {
        type: game,
        issue: issue,
    }, cb);
}

/**
 * 查看某期的输赢结果
 */
cmd.prototype.profit = function(game, issue, cb) {
    var self = this;
    self.post("bet/profit", {
        type: game,
        issue: issue,
    }, cb);
}

/**
 * 统计今日的输赢信息
 */
cmd.prototype.find_bet_profit = function(id, game, cb, fail) {
    var self = this;
    var data = {
        type: game,
    };
    if (id) data.uid = id;
    self.post("bet/profit/find", data, cb, fail);
}

/**
 * 抓取所有赢的下注列表
 */
cmd.prototype.query_bet_win = function(cb) {
    var self = this;
    self.post("bet/win/query", {}, cb);
}

/**
 * 获取某个游戏的赔率信息
 */
cmd.prototype.query_lottery_rate = function(game, owner, cb) {
    var self = this;
    self.post("lottery/rate/query", {
        type: game,
        owner: owner || "",
    }, cb);
}

/**
 * 取款
 */
cmd.prototype.withdraw = function(id, owner, amount, memo, cb, fail) {
    var self = this;
    if (memo && typeof(memo) != 'string') memo = JSON.stringify(memo);
    self.post("account/withdraw", {
        user: id,
        amount: amount,
        memo: memo,
        owner: owner || '',
    }, cb, fail);
}

/**
 * 刷新我的账户信息
 */
cmd.prototype.refresh_me = function(uid, owner, success, fail) {
    var self = this;
    var data = {};
    if (uid) data.uid = uid;
    data.owner = owner || "";
    self.post("account/query", data, success, fail);
}

/**
 * 获取某个用户的账户信息
 */
cmd.prototype.query_account = function(uid, owner, cb) {
    var self = this;
    var data = {};
    if (uid) data.uid = uid;
    //data.owner = owner || "";
    self.post("account/query-uid", data, cb);
}

/**
 * 惩罚扣款
 */
 cmd.prototype.account_penalty = function(id, owner, amount, memo, cb) {
     var self = this;
     if (memo) {
        if (typeof(memo) == 'string')
            memo = memo;
        else
            memo = JSON.stringify(memo);
    }
     self.post("account/penalty", {
        user: id,
        owner: owner || "",
        amount: amount,
        memo: memo,
     }, cb);
}
cmd.prototype.account_gpenalty = function(id, owner, amount, memo, cb) {
     var self = this;
     self.post("account/gpenalty", {
        user: id,
        owner: owner || "",
        amount: amount,
        memo: memo,
     }, cb);
}

/**
 * 其他扣款
 */
cmd.prototype.account_deduct = function(id, owner, amount, memo, cb) {
    var self = this;
    if (memo) {
       if (typeof(memo) == 'string')
           memo = memo;
       else
           memo = JSON.stringify(memo);
   }
    self.post("account/deduct", {
       user: id,
       owner: owner || "",
       amount: amount,
       memo: memo,
    }, cb);
}

/**
 * 修改用户信息
 */
cmd.prototype.modify_user_info = function(id, data, cb) {
    var self = this;
    if (id) data.id = id;
    self.post("user/modify", data, cb);
}

/**
 * 请求试玩
 */
cmd.prototype.trial = function(cb) {
    var self = this;
    self.post("trial/deposit", {}, cb);
}

/**
 * 我创建的群列表
 */
cmd.prototype.group_query_owner = function(cb) {
    var self = this;
    self.post("group/query-owner", {}, cb);
}

/**
 * 账户注册
 */
cmd.prototype.register = function(uid, password, realname, qq, referee, source, success, fail) {
    var self = this;
    var data = {
        type: 1,
        uid: uid,
        password: password,
        source: source || 'pc',
    };
    if (realname) data.name = realname;
    if (referee) data.referee = referee;
    self.post("user/register", data, function(r) {
        // 修改qq等信息
        if (qq)
            CMD.modify_user_info(null, { qq: qq }, function(r) {
                context.user.user = r;
            });

        success(r);
    }, function(message) {
        fail(message);
    });
}

/**
 * 取得服务器时间
 */
cmd.prototype.server_time = function(cb) {
    var self = this;
    self.post("tephra/ctrl/status", {}, cb);
}

/**
 * 查询IP信息
 */
cmd.prototype.ip_info = function(ip, cb) {
    var self = this;
    self.post("user/ip", {
        addr: ip,
    }, function(r) {
        if (r.country == '中国')
            r.addr = (r.province || '') + (r.city || '');
        else
            r.addr = (r.country || '') + (r.province || '') + (r.city || '');
        cb(r);
    });
}

/**
 * 合易付充值
 */
cmd.prototype.pay_heyifuu_create = function(key, amount, summary, channel3, cb) {
    var self = this;
    self.post("pay/heyifuu/create", {
        key: key,
        amount: amount,
        summary: summary || '',
        channel3: channel3 || '',
    }, cb);
}

/**
 * 闪付充值
 */
cmd.prototype.pay_sslsf_create = function(key, amount, payType, channel3, cb) {
    var self = this;
    var data = {
        key: key,
        amount: amount,
        channel3: channel3 || '',
    };
    if (payType) data.payId = payType;
    self.post("pay/sslsf/create", data, cb);
}

/**
 * 新码支付
 */
cmd.prototype.pay_xinmapay_create_qr = function(key, amount, payType, prodName, channel3, cb) {
    var self = this;
    var data = {
        key: key,
        amount: amount,
        channel3: channel3 || '',
    };
    if (payType) data.payType = payType;
    if (prodName) data.prodName = prodName;
    self.post("pay/xinmapay/create-qr", data, cb);
}
cmd.prototype.pay_xinmapay_create_pc = function(key, amount, payType, prodName, channel3, cb) {
    var self = this;
    var data = {
        key: key,
        amount: amount,
        channel3: channel3 || '',
        messageid: '200002',
        bankCode: 'ICBCD',
        bankFlag: 0,
        propDesc: 'goods',
    };
    if (payType) data.payType = payType;
    if (prodName) data.prodName = prodName;
    self.post("pay/xinmapay/create-pc", data, cb);
}
cmd.prototype.pay_xinmapay_create_h5 = function(key, amount, payType, prodName, channel3, cb) {
    var self = this;
    var data = {
        key: key,
        amount: amount,
        channel3: channel3 || '',
    };
    if (payType) data.payType = payType;
    if (prodName) data.prodName = prodName;
    self.post("pay/xinmapay/create-h5", data, cb);
}

cmd.prototype.pay_uquery = function(cb) {
    var self = this;
    self.post("pay/query-user", {
        pageNum: 1,
        pageSize: 200,
    }, cb);
}

cmd.prototype.post = function(command, data, success, fail, end) {
    var self = this;
    self.count++;
    $.ajax({
        url: Util.getUrl(command),
        type: "POST",
        dataType: "json",
        //contentType: "application/json;charset=utf-8",
        data: data,
        async: true,
        success: function(r) {
            console.log("cmd=" + command, r);
            if (r.code != 0 && fail) {
                fail(r.message);
                return;
            }
            else if (success) {
                var ti = new Date().getTime();
                if (r.data === undefined) success(r);
                else success(r.data);
                ti = new Date().getTime() - ti;
                if (ti > 10)
                    console.log("耗时：", ti);////----
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            if (fail) fail();
        },
        complete: function(r) {
            self.count--;
            if (end) end();
        },
    });
}

window.CMD = new cmd();